<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-24 14:14
 *
 * Version: 2.0
 */

namespace App\Modules\Message\Services;


use App\Lib\Utilities;
use App\Modules\Message\Criterias\GetWxMessageListCriteria;
use App\Modules\Message\Repositories\MessageLogRepository;
use App\Modules\Message\Repositories\MessageRepository;
use App\Modules\Order\Repositories\OrderRepository;
use Flower\Core\Repository\Eloquent\BaseService;

class MessageService extends BaseService
{
	private $messageRepository;
	private $messageLogRepository;
	private $orderRepository;

	public function __construct(MessageRepository $messageRepository,MessageLogRepository $messageLogRepository,OrderRepository $orderRepository)
	{
		$this->messageRepository = $messageRepository;
		$this->messageLogRepository = $messageLogRepository;
		$this->orderRepository =$orderRepository;
	}

	/**
	 * 消息通知列表
	 * @param $recipient
	 * @param $scene
	 * @param $type
	 * @param $pageSize
	 * @param bool $isApp
	 * @param null $storeId
	 * @return array|mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getList($recipient, $scene, $type,$pageSize,$isApp=true,$storeId=null){
		$pageSize = isset($pageSize) ? $pageSize : 10;
		$list = $this->messageLogRepository->getMessageList($recipient,$scene,$type,$pageSize,$isApp,$storeId);
		$list = Utilities::dealPage($list);
		$messageCount = $this->getMessageUnReadNumAndOrderNum($recipient,$scene, $type,$isApp,$storeId);
		$list['count_is_not_read'] = $messageCount['count_is_not_read'];
		$list['order_doing'] = $messageCount['order_doing'];
		// 统计未读的总数
		return $list;
	}

	/**
	 * 统计未读消息数量和正在进行中的订单
	 * @param $recipient
	 * @param $scene
	 * @param $type
	 * @param bool $isApp
	 * @param null $storeId
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getMessageUnReadNumAndOrderNum($recipient,$scene, $type,$isApp=true,$storeId=null){
		$count = $this->messageLogRepository->countMessageList($recipient,$scene,$type,$isApp,$storeId);
		$count = empty($count->toArray()) ? [] : array_column($count->toArray(),null,'is_read');
		$list['count_is_not_read'] = empty($count) || empty( $count[config('constant.no')]) ? 0 :  $count[config('constant.no')]['count'];
		$list['order_doing'] = $this->orderRepository->getMemberOrderUnfinishedNum($recipient);
		return $list;
	}





	/**
	 * 信息数据统计
	 * @param $recipient
	 * @param $scene
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getAppCountData($recipient,$scene){
		$count = $this->messageLogRepository->countMessageCounts($recipient,$scene);
		$newCount = null;
		if(isset($count)){
			foreach ($count as &$v){
				if($v->type > 0){
					$newCount[] = [
						'type' => $v->type,
						'read_count' => $v->read_count,
						'unread_count' => $v->unread_count
					];
				}
			}
			unset($v);
		}
		return isset($newCount) ? $newCount : null;
	}
	/**
	 * 信息已读接口
	 * @param $messageId
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function readMessage($messageId){
		$this->messageLogRepository->isExisted($messageId);
		return $this->messageLogRepository->save($messageId);
	}


	public function readAll($uerIdOrMemberId ,$scene){
		return $this->messageLogRepository->readAll($uerIdOrMemberId,$scene);
	}


	/**
	 * 未读消息总数
	 * @param $recipient
	 * @param $scene
	 * @return mixed
	 */
	public function getMemberUnreadNum($recipient,$scene){
		return $this->messageLogRepository->getUnreadNum($recipient,$scene);
	}
}